<?php
	$serviceMethod = getInput("method", "");
	if (function_exists($serviceMethod)){
		include_once ('mapper/baseMapper.php');
		includeFolder("service/mapper");

		$serviceMethod();
	}else{
		response("","",false, langEcho("service:not:exist"));
	}

	function response($key="", $object="", $success = true, $message = ""){
		$return = new stdClass();
		$return->success = $success;
		$return->message = $message;
		if($key != ""){
			$return->$key = $object;
		}

		$returnJson =  json_encode($return);

		echo $returnJson;
		exit;
	}

	function getBenefitsByCategory(){
		$cid = getInput("cid", -1);
		$lon = getInput("lon", null);
		$lat = getInput("lat", null);
		$benefit = new Benefit();
		$benefits = $benefit->getByCategoryId($lon, $lat, $cid);

		$mapper = new BenefitMapper();
		$mapper->getBenefits();
		response("benefits", $mapper->objectMapper($benefits));

	}

	function getBenefitsByText(){
		$text = getInput("text", "");
		$lon = getInput("lon", null);
		$lat = getInput("lat", null);
		$benefit = new Benefit();
		$benefits = $benefit->getByText($lon, $lat, $text);

		$mapper = new BenefitMapper();
		$mapper->getBenefits();
		response("benefits", $mapper->objectMapper($benefits));

	}

	function getTopRankBenefits(){
		$lon = getInput("lon", null);
		$lat = getInput("lat", null);
		$benefit = new Benefit();
		$benefits = $benefit->getTopRanked(null,null,$lon, $lat, 20,0);
		array_splice($benefits, 5);

		$mapper = new BenefitMapper();
		$mapper->getBenefits();
		response("benefits", $mapper->objectMapper($benefits));

	}


	function getCategories(){
		$category = new Category();
		$categories = $category->getAll("name asc","","","", true);

		$mapper = new CategoryMapper();
		$mapper->getCategories();
		response("categories", $mapper->objectMapper($categories));
	}

	function isAlreadyLiked(){
		$userId = getInput("userId",0);
		$benefitId = getInput("benefitId",0);
		if(!$userId || !$benefitId){
			response("", "", false, "Por favor complete el usuario y beneficio");
		}

		$like = new Likes();

		response("response", $like->isAlreadyLiked($benefitId, $userId));
	}

	function getLikes(){
		$benefitId = getInput("benefitId",0);
		if(!$benefitId){
			response("", "", false, "Por favor complete el beneficio");
		}

		$like = new Likes();

		response("response", $like->countByBenefitId($benefitId));
	}

	function likeBenefit(){
		$userId = getInput("userId",0);
		$benefitId = getInput("benefitId",0);
		if(!$userId || !$benefitId){
			response("", "", false, "Por favor complete el usuario y beneficio");
		}

		$like = new Likes();
		if($like->insertFieldsFromRequest()){
			response();
		}else{
			response("", "", false, "No se pudo realizar su gusto");
		}

	}

	function login(){
		$email = getInput("email");
		$password = getInput("password");

		$user = new User();
		if($user->login($email, $password)){
			response("userId", getUserLoggedInId());
		}else{
			response("", "", false, "Email o contraseña incorrectos");
		}

	}

?>